/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import beans.B_Cliente;
import beans.B_Persona;
import beans.B_Sector;
import conexion.Conexion;
import java.sql.Types;

/**
 *
 * @author Elizabeth
 */
public class DB_Cliente {

    public static ArrayList<B_Cliente> getList(int id) {
        ArrayList<B_Cliente> list = new ArrayList<B_Cliente>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;
            if (id == 0) {
                ps = con.prepareStatement(" select c.id, c.razon_social, c.ruc, c.id_sector, s.descripcion as sector, c.id_persona, p.apellidos, p.nombres from tbl_cliente c inner join tbl_sector s on s.id = c.id_sector inner join tbl_persona p on p.id = c.id_persona where c.removed = false order by c.razon_social ");
            } else {
                ps = con.prepareStatement(" select c.id, c.razon_social, c.ruc, c.id_sector, s.descripcion as sector, c.id_persona, p.apellidos, p.nombres from tbl_cliente c inner join tbl_sector s on s.id = c.id_sector inner join tbl_persona p on p.id = c.id_persona where c.removed = false and c.id = " + id);
            }
            rs = ps.executeQuery();
            B_Cliente obj;
            B_Sector objSector;
            B_Persona objPersona;
            while (rs.next()) {
                obj = new B_Cliente();
                objSector = new B_Sector();
                objPersona = new B_Persona();
                //Asign values
                obj.setId(rs.getInt("id"));
                obj.setRazonSocial(rs.getString("razon_social"));
                obj.setRuc(rs.getString("ruc"));
                objSector.setId(rs.getInt("id_sector"));
                objSector.setDescripcion(rs.getString("sector"));
                obj.setObjSector(objSector);
                objPersona.setId(rs.getInt("id_persona"));
                objPersona.setApellidos(rs.getString("apellidos"));
                objPersona.setApellidos(rs.getString("nombres"));
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    //SP CRUD
    public static String crud(B_Cliente obj, String action) {
        String result = "";
        CallableStatement cs = null;
        Connection con = Conexion.getConexion();
        try {
            cs = con.prepareCall("{call sp_crud_cliente(?,?,?,?,?,?,?,?)}");

            cs.registerOutParameter("p_message", Types.VARCHAR);
            cs.setString("p_action", action);
            cs.setInt("p_id", obj.getId());
            cs.setString("p_razon_social", obj.getRazonSocial());
            cs.setString("p_ruc", obj.getRuc());
            cs.setInt("p_id_sector", obj.getObjSector().getId());
            cs.setInt("p_id_persona", obj.getObjPersona().getId());
            cs.setString("p_user", obj.getUser());
            cs.executeUpdate();
            result = cs.getString(1);
        } catch (Exception ex) {
            result = ex.getMessage();
        } finally {
            try {
                cs.close();
                con.close();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        return result;
    }
}
